package com.sktlab.bizconfmobile.activity;

import java.util.ArrayList;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sktlab.bizconfmobile.R;
import com.sktlab.bizconfmobile.adapter.ContactListAdapter;
import com.sktlab.bizconfmobile.interfaces.IOnCheckedListener;
import com.sktlab.bizconfmobile.model.ConfControl;
import com.sktlab.bizconfmobile.model.ContactItem;
import com.sktlab.bizconfmobile.model.Participant;
import com.sktlab.bizconfmobile.model.manager.ContactManager;
import com.sktlab.bizconfmobile.util.Util;
import com.sktlab.bizconfmobile.util.ValidatorUtil;

public class AddParticipantActivity extends BaseActivity implements IOnCheckedListener{
	
	public static final String TAG = "AddParticipantActivity";
	
	public static final String KEY_OF_SHOW_EMAIL_ADDRESS = "com.sktlab.show.email.key";
	private ExpandableListView mExpandListview;
	private ContactListAdapter mContactListAdapter;
		
	private ArrayList<ContactItem> mContacts;
	private LinearLayout mSelectedPartLayout;
	
	private Context mCtx;
	private LayoutInflater mInflater;
	
	private HorizontalScrollView mHsView;
	
	private EditText mEtInputNumber;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activtiy_add_participant);		
		
		init();
	}

	@Override
	protected void onResume() {
		super.onResume();			
	}
	
	
	/**
	 * should add selected party to conference
	 * 
	 * we can get it from ContactManager.getInstance().getAllSelectedContacts()
	 */
	@Override
	public void onRightButtonClicked(View v) {

		//add contact to conference code here
		//...
		
		do {
			
			ArrayList<Participant> parties = 
					ContactManager.getInstance().getAllSelectedContacts();
			
			if (parties.size() == 0) {
				
				break;
			}
			
			for (Participant party : parties){
				
				ConfControl.getInstance().addPartyToConf(party, false);
			}						
			
		}while(false);
		
		
		finish();
	}

	public void init(){
		
		mCtx = this;
		
		setShowRightButton(true);
		
		mInflater = (LayoutInflater)mCtx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mContacts = ContactManager.getInstance().getAllContacts();
		
		mSelectedPartLayout = (LinearLayout)findViewById(R.id.layout_select_contact);
		mExpandListview = (ExpandableListView) findViewById(R.id.elv_contact_list);
		
		boolean isShowEmail = getIntent().getBooleanExtra(KEY_OF_SHOW_EMAIL_ADDRESS, false);
		
		mContactListAdapter = 
				new ContactListAdapter(this, mContacts, isShowEmail);
		
		mExpandListview.setAdapter(mContactListAdapter);
		mContactListAdapter.setCallback(this);	
		mExpandListview.setOnChildClickListener(mContactListAdapter);
		//this method make group can not answer user's click
		mExpandListview.setOnGroupClickListener(mContactListAdapter);
		
		int groupCount = mExpandListview.getCount();
		
		for(int i = 0; i < groupCount; i++) {
			
			mExpandListview.expandGroup(i);
		}
		
		mHsView = (HorizontalScrollView) findViewById(R.id.hs_add_party);
		
		LinearLayout layoutInputNumber = (LinearLayout) findViewById(R.id.layout_input_num);
		mEtInputNumber = (EditText) layoutInputNumber.findViewById(R.id.et_for_input);
		
		showSelectedParty();
	}
	
	/**
	 * when user checked or unchecked a a party 
	 */
	@Override
	public void onChecked() {
		showSelectedParty();
	}
	
	public void showSelectedParty() {
		
		ArrayList<Participant> participants = 
				ContactManager.getInstance().getAllSelectedContacts();
		
		int partCount = participants.size();
		
		if(partCount > 0) {
			
			mSelectedPartLayout.setVisibility(View.VISIBLE);
		}else {
			
			mSelectedPartLayout.setVisibility(View.GONE);
		}
		
		//remove all views added before
		mSelectedPartLayout.removeAllViews();
		
		for(int i =0; i < partCount;i++){		
			
			Participant participant = participants.get(i);
			//show the specified participant
			TextView tvName = getTextView();		
			tvName.setText(participant.getName());
			tvName.setVisibility(View.VISIBLE);
			tvName.setOnClickListener(onItemClickListener);
			
			//set the participant as tag of the text view, we will use it when user click the textview
			tvName.setTag(participant);
			
			mSelectedPartLayout.addView(tvName);						
		}		
        
		//use this method to have a full scroll
		mHsView.post(new Runnable() {
			
			@Override
			public void run() {
			
				mHsView.fullScroll(HorizontalScrollView.FOCUS_RIGHT);
			}
		});
		
		mContactListAdapter.notifyDataSetChanged();
	}
	
	private TextView getTextView() {

		LinearLayout layout = (LinearLayout)mInflater.inflate(R.layout.item_participant_list, null);
		
		TextView tv = (TextView)layout.findViewById(R.id.tv_1);
		
		layout.removeView(tv);
		return tv;
	}

	private OnClickListener onItemClickListener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			
			//Util.BIZ_CONF_DEBUG(TAG, "onItemClicked called");
			
			Participant item = (Participant)v.getTag();
			
			if(!Util.isEmpty(item)) {
				
				int conatactId = item.getContactId();
				int selectedAttrPos = item.getSelectedAttrPosInContactItem();
				
				ContactManager cm = ContactManager.getInstance();
				cm.removeSelectedContact(item);
				
				ContactItem contact = cm.getContactById(conatactId);
				contact.setAttrSelectedState(selectedAttrPos, false);
				
				onChecked();
			}	
		}
	};
	
	public void onInputNumConfirmClicked(View v) {
		
		String phoneNumber = mEtInputNumber.getText().toString();
		
		boolean isValid = ValidatorUtil.isPhoneNumberValid(phoneNumber);
		
		if(!isValid) {
			
			Util.shortToast(mCtx, R.string.toast_phone_num_invalid);
		}
	}
}
